import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  ManyToMany,
  JoinTable,
  CreateDateColumn,
  UpdateDateColumn,
  OneToMany,
} from 'typeorm';
import { Article } from './article.entity';
import { Exclude } from 'class-transformer';
import { Role } from './role.entity';

@Entity()
export class User {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ unique: true })
  account: string;

  @Column()
  password: string;

  @Column()
  userName: string;

  @Column({ unique: true })
  phoneNum: string;

  @Column({ default: '' })
  avatar: string;

  @Column({ default: true })
  isActive: boolean;

  @Column({ default: '' })
  desc: string;

  @CreateDateColumn()
  createTime: Date;

  @UpdateDateColumn()
  updateTime: Date;

  @OneToMany((type) => Article, (article) => article.user, {
    cascade: true,
    onDelete: 'CASCADE',
  })
  articls: Article[];

  @ManyToMany((type) => Article, (article) => article.collectUsers, {
    cascade: true,
  })
  @JoinTable()
  collectArticles: Article[];

  @ManyToMany((type) => Role, (role) => role.users, {
    cascade: true,
    onDelete: 'CASCADE',
  })
  @JoinTable()
  roles: Role[];
}
